package ls.jpa.chapterA.service.impl;

import com.mysema.query.types.expr.BooleanExpression;
import ls.jpa.chapterA.domain.Customer;
import ls.jpa.chapterA.domain.QCustomer;
import ls.jpa.chapterA.repository.CustomerRepository;
import ls.jpa.chapterA.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Barudisshu
 */
@Service("customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @Override
    public List<Customer> findByFirstName(String firstName){
        return customerRepository.findByFirstName(firstName);
    }

    @Override
    public List<Customer> findByLastName(String lastName){
        return customerRepository.findByLastName(lastName);
    }

    @Override
    public List<Customer> findByVersion(Long version){
        return customerRepository.findByVersion(version);
    }

    @Override
    public Customer findOne(String emailAddress){
        QCustomer customer = QCustomer.customer;
        BooleanExpression predicate = customer.emailAddress.eq(emailAddress);
        return (Customer)customerRepository.findOne(predicate);
    }
}
